/*
 * I M P O R T A N T
 * -----------------
 * This file contains a lot of stuff I just took of the web to try it
 * Due to that fact I do not claim ownership of this file
 * As far as I could see there were no notes about license at the sites so use carefully
 * I'll clear this file out later, but right now I need to put it up to get access to it on my other computer
 */


package swengine;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



public class Main
{
	public static void main(String[] args)
	{
		boolean fullscreen = false;

		final JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("SwedTech preview");

		if (args.length > 0 && args[0].equalsIgnoreCase("--fullscreen"))
		{
			try
			{
				frame.setUndecorated(true);
				frame.setResizable(false);
				frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
				fullscreen = true;
			}
			catch (Exception ex)
			{
				JOptionPane.showMessageDialog(frame, "Failed to enable fullscreen");
				ex.printStackTrace();
				System.exit(1);
			}
		}
		else
		{
			frame.setSize(200, 200);
		}

		RotatePanel pic = new RotatePanel(Toolkit.getDefaultToolkit().createImage(Main.class.getResource("test.png")), fullscreen);
		pic.setFocusable(true);

		pic.addKeyListener(new KeyListener()
		{
			boolean on;

			public void keyTyped(KeyEvent e) {}

			public void keyPressed(KeyEvent e)
			{
				if (e.getKeyCode() == KeyEvent.VK_ENTER && !on)
				{
					on = true;
					new SSound(Main.class.getResource("test.wav")).play();
				}
			}

			public void keyReleased(KeyEvent e)
			{
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					on = false;
				}
				else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
				{
					System.exit(0);
				}
			}
		});

		new SAddon().load();

		frame.add(pic);
		
		frame.setVisible(true);

		new SSound(Main.class.getResource("test.wav")).play();

		while (true)
		{
			pic.rotate();
			try
			{
				Thread.sleep(5);
			}
			catch (InterruptedException ex)
			{
				ex.printStackTrace();
				System.exit(1);
			}
		}
	}
}

class RotatePanel extends JPanel {
   private Image image;
   private double currentAngle;
   private boolean full;

   public RotatePanel(Image image, boolean full) {
     this.image = image;
	 this.full = full;
     MediaTracker mt = new MediaTracker(this);
     mt.addImage(image, 0);
     try {
       mt.waitForID(0);
     }
     catch (Exception e) {
       e.printStackTrace();
     }
   }

   private void write(Graphics2D g, String text, int x, int y)
   {
		Color org = g.getColor();
		g.setColor(Color.WHITE);
		g.drawString(text, x + 1, y + 1);
		g.setColor(Color.BLACK);
		g.drawString(text, x, y);
		g.setColor(org);
   }

   public void rotate() {
     //rotate 5 degrees at a time
     currentAngle+=0.5;
     if (currentAngle >= 360.0) {
       currentAngle = 0;
     }
     repaint();
   }
   protected void paintComponent(Graphics g) {
     super.paintComponent(g);
     Graphics2D g2d = (Graphics2D)g;
     AffineTransform origXform = g2d.getTransform();
     AffineTransform newXform = (AffineTransform)(origXform.clone());
     //center of rotation is center of the panel
     int xRot = this.getWidth()/2;
     int yRot = this.getHeight()/2;
     newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
     g2d.setTransform(newXform);
     //draw image centered in panel
     int x = (getWidth() - image.getWidth(this))/2;
     int y = (getHeight() - image.getHeight(this))/2;
     g2d.drawImage(image, x, y, this);
     g2d.setTransform(origXform);

	 Point m = MouseInfo.getPointerInfo().getLocation();
	 
	 String loc = "X: " + m.x + "     " +
				  "Y: " + m.y;

	 write(g2d, loc, 7, 10);

	 write(g2d, "Angle: " + Math.round(currentAngle), 7, 20);

	 write(g2d, "Try pressing enter!", 7, 30);

	 if (full)
	 {
		 write(g2d, "Press ESCAPE to close this window!", 7, 40);
	 }
   }

   @Override
   public Dimension getPreferredSize() {
     return new Dimension (image.getWidth(this), image.getHeight(this));
   }
}
